<template>
  <div id="app">
    <keep-alive include="LayoutPage">
      <router-view></router-view>
    </keep-alive>
  </div>
</template>

<script>
import { mapMutations } from 'vuex'
// import { loginGetCode2, loginSystem2, loginGetCode, loginSystem } from '@/apis/login'
import wx from 'weixin-js-sdk'; // 引入wxjs
export default {
  methods: {
    ...mapMutations('user', ['updateUserInfo']),
    toShareWx() {
      var broser = window.navigator.userAgent.toLowerCase();
      //通过正则表达式匹配broser 中是否含有MicroMessenger字符串
      if (broser.match(/MicroMessenger/i) == 'micromessenger') {
        this.getShareInfo()
      } else {
        this.$toast.fail({
          duration: 1000, // 持续展示 toast
          forbidClick: true,
          message: '请使用微信浏览器打开',
        });
      }
    },
    // 生成字符随机串
    generateRandomString(length) {
      var result = '';
      var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
      var charactersLength = characters.length;
      for (var i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
      }
      return result;
    },
    // vue调用微信的自定义分享
    getShareInfo() {
      this.$axios.get(`front/share`).then((res) => {
        const code = res.code
        const result = res.result
        console.log(443, new Date().getTime(), this.generateRandomString(10));
        if (code == 0) {
          this.save = {
            url: location.href.split('#')[0] // 只需要传当前页面地址
          };
          wx.config({
            appId: 'wxe07d768775a370d4',  // appID 公众号的唯一标识
            timestamp: new Date().getTime(), // 生成签名的时间戳
            nonceStr: this.generateRandomString(10), //  生成签名的随机串
            signature: result, // 生成的签名
            jsApiList: [
              'updateAppMessageShareData', // 分享到朋友
              'updateTimelineShareData', // 分享到朋友圈
            ]
          });

          wx.ready(() => {
            var shareData = {
              title: '舞匣',
              desc: '舞匣极简打分器',
              link: 'https://test.dancebox.cn/html/static/H5scoring/index.html#/home', // 分享后的地址
              imgUrl: '@/assets/multif.jpg'
            };
            //点击要去分享
            wx.updateAppMessageShareData(shareData);
            wx.updateTimelineShareData(shareData);
          });
        } else {
          // alert('分享失败');
        }
        wx.error(res => {
          console.log("wx.config error:", res);
          // if ( config_num < 10) {
          //   config_num++
          //   _wxConfig(config)
          // }
        })
      });
    },
  },
  mounted() {
    if (localStorage.getItem('shop_userinfo') && localStorage.getItem('shop_userinfo').length > 2) {
      this.updateUserInfo(localStorage.getItem('shop_userinfo'))
    }
  },
  async created() {
    this.toShareWx()
    // await loginGetCode()
    // const res = await loginSystem()
    // console.log(res,1)
    // this.updateUserInfo(res.result)
  }
}
</script>

<style lang="less" scoped></style>

<style lang="less">
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  //text-align: center;
  color: #2c3e50;
  background: #1e1e35;
  overflow: hidden auto;
  // min-height: 100vh;
  height: 100%;
}

nav {
  padding: 30px;

  a {
    font-weight: bold;
    color: #2c3e50;

    &.router-link-exact-active {
      color: #42b983;
    }
  }
}
</style>
